const express = require("express");
const fs = require("fs");
const path = require("path");
const router = express.Router();
const {
  Document,
  Paragraph,
  TextRun,
  Table,
  TableRow,
  TableCell,
  Packer,
  WidthType,
} = require("docx");

// 表格总宽度
const TABLE_WIDTH = 10000; // 约17.6cm，A4纸宽度
// 各列宽度分配（单位：dxa）
const colWidths = [1000, 2000, 2000, 2000, 1000, 2000];

// 表格单元格样式
const tableCellStyle = {
  borders: {
    top: { style: "single", size: 2, color: "000000" },
    bottom: { style: "single", size: 2, color: "000000" },
    left: { style: "single", size: 2, color: "000000" },
    right: { style: "single", size: 2, color: "000000" },
  },
  verticalAlign: "center",
  margins: { top: 100, bottom: 100 },
};

// 表格头部文本样式
const tableHeaderText = (text) =>
  new Paragraph({
    children: [new TextRun({ text, bold: false, font: "SimSun", size: 24 })],
    alignment: "center",
  });

// 表格单元格文本样式
const tableCellText = (text) =>
  new Paragraph({
    children: [new TextRun({ text, font: "SimSun", size: 22 })],
    alignment: "center",
  });

router.post("/", async (req, res) => {
  try {
    const {
      应聘岗位 = "",
      姓名 = "",
      性别 = "",
      出生年月 = "",
      民族 = "",
      籍贯 = "",
      政治面貌 = "",
      参加工作时间 = "",
      专业技术职务 = "",
      执业资格 = "",
      手机 = "",
      身份证号码 = "",
      全日制学历 = "",
      全日制学位 = "",
      全日制院校 = "",
      在职学历 = "",
      在职学位 = "",
      在职院校 = "",
      现工作单位及职务 = "",
      任现职时间 = "",
      学历教育 = "",
      培训经历 = "",
      工作经历 = "",
      近年主要工作业绩 = "",
      奖惩情况 = "",
    } = req.body;

    const doc = new Document({
      sections: [
        {
          children: [
            // 标题
            new Paragraph({
              children: [
                new TextRun({
                  text: "“活水计划”内部招聘报名表",
                  bold: true,
                  size: 36,
                }),
              ],
              alignment: "center",
              spacing: { after: 100 },
            }),
            // 说明
            new Paragraph({
              children: [
                new TextRun({
                  text: "（本表是资格审核的重要依据，请如实、准确、完整、清晰填写）",
                  size: 20,
                }),
              ],
              alignment: "center",
              spacing: { after: 200 },
            }),
            // 应聘岗位
            new Paragraph({
              children: [
                new TextRun({
                  text: `应聘岗位：${应聘岗位}`,
                  bold: true,
                  size: 24,
                }),
              ],
              spacing: { after: 100 },
            }),
            // 表格第一页内容
            new Table({
              columnWidths: colWidths,
              width: { size: TABLE_WIDTH, type: "dxa" },
              rows: [
                // 第一行
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  width: { size: TABLE_WIDTH, type: "dxa" },
                  children: [
                    new TableCell({
                      children: [tableHeaderText("姓名")],
                      width: { size: colWidths[0], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(姓名)],
                      width: { size: colWidths[1], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText("出生年月")],
                      width: { size: colWidths[2], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(出生年月)],
                      width: { size: colWidths[3], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText("性别")],
                      width: { size: colWidths[4], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(性别)],
                      width: { size: colWidths[5], type: "dxa" },
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第二行
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableHeaderText("民族")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(民族)],
                      width: { size: colWidths[1], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText("籍贯")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(籍贯)],
                      width: { size: colWidths[3], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText("政治面貌")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(政治面貌)],
                      width: { size: colWidths[5], type: "dxa" },
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第三行
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableHeaderText("参加工作时间")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(参加工作时间)],
                      width: { size: colWidths[1], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText("专业技术职务")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(专业技术职务)],
                      width: { size: colWidths[3], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText("执（职）业资格")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(执业资格)],
                      width: { size: colWidths[5], type: "dxa" },
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第四行
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableHeaderText("手机")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(手机)],
                      width: { size: colWidths[1], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText("身份证号码")],

                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableHeaderText(身份证号码)],
                      width: { size: colWidths[3], type: "dxa" },
                      columnSpan: 3,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第五行 学历学位
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableHeaderText("学历学位")],
                      rowSpan: 4,
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("全日制教育")],
                      rowSpan: 2,
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("（请填写学历）")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("院校系及专业")],
                      rowSpan: 2,
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("（请填写毕业院校）")],
                      columnSpan: 2,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("（请填写学位）")],
                      ...tableCellStyle,
                    }),

                    new TableCell({
                      children: [tableCellText("（请填写学历）")],
                      columnSpan: 2,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("在职教育")],
                      rowSpan: 2,
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("（请填写学历）")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("院校系及专业")],
                      rowSpan: 2,
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("（请填写毕业院校）")],
                      columnSpan: 2,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("（请填写学位）")],
                      ...tableCellStyle,
                    }),

                    new TableCell({
                      children: [tableCellText("（请填写毕专业）")],
                      columnSpan: 2,
                      ...tableCellStyle,
                    }),
                  ],
                }),

                // 第七行 现工作单位及职务
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("现工作单位及职务")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                      columnSpan: 2,

                      width: { size: colWidths[1], type: "dxa" },
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("任现职时间")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                      columnSpan: 2,

                      width: { size: colWidths[3], type: "dxa" },
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第八行 学历教育
                new TableRow({
                  height: { value: 1500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("学历教育")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [
                        tableCellText(
                          "（请填写学历教育时间、院校及专业，例：2008.09-2012.07 XX大学 XX专业）"
                        ),
                      ],
                      columnSpan: 5,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第九行 培训经历
                new TableRow({
                  height: { value: 1500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("培训经历")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [
                        tableCellText(
                          "（请填写培训时间、机构及内容，例：2013.09-2013.10 XX培训班）"
                        ),
                      ],
                      columnSpan: 5,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第10行 工作经历
                new TableRow({
                  height: { value: 1500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("工作经历")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [
                        tableCellText(
                          "（请填写工作经历时间、单位及职务，例：2013.09-2013.10 XX单位 XX职务）"
                        ),
                      ],
                      columnSpan: 5,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第11行 近年主要工作业绩
                new TableRow({
                  height: { value: 1500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("近年主要工作业绩")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [
                        tableCellText(
                          "（请填写近年主要工作业绩，例：2013.09 XX奖项 XX等级）"
                        ),
                      ],
                      columnSpan: 5,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第12行 奖惩情况
                new TableRow({
                  height: { value: 1500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("奖惩情况")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [
                        tableCellText("（请填写奖励获得时间、名称及授予单位）"),
                      ],
                      columnSpan: 5,
                      ...tableCellStyle,
                    }),
                  ],
                }),
                // 第13行及其后所有 TableRow
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("家庭成员及主要社会关系")],
                      // rowSpan: 7,
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("称谓")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("姓名")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("出生年月")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("政治面貌")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("工作单位及职务")],
                      ...tableCellStyle,
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                  ],
                }),
                new TableRow({
                  height: { value: 500, rule: "atLeast" },
                  children: [
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                      ...tableCellStyle,
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                    new TableCell({
                      children: [tableCellText("")],
                    }),
                  ],
                }),
              ],
            }),
          ],
        },
      ],
    });

    // 生成Word文档
    const buffer = await Packer.toBuffer(doc);

    // 设置响应头，对中文文件名进行编码
    const filename = encodeURIComponent("活水计划内部招聘报名表.docx");
    res.setHeader(
      "Content-Type",
      "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
    );
    res.setHeader(
      "Content-Disposition",
      `attachment; filename="${filename}"; filename*=UTF-8''${filename}`
    );
    res.setHeader("Content-Length", buffer.length);

    // 发送文件
    res.send(buffer);
  } catch (error) {
    console.error("生成Word文档时出错:", error);
    res.status(500).json({
      success: false,
      message: "生成Word文档失败",
      error: error.message,
    });
  }
});

module.exports = router;
